<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PublicController extends Controller
{

    protected $redirectTo = '/admin';

    /**
     * 构造函数
     * 排除当前控制器授权验证
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * 登陆
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View|void
     */
    public function login(Request $request)
    {
       if($request->getMethod()=='POST'){
           //模拟注册一个管理员
//           $admin = \App\Admin::create([
//               'name' => 'admin',
//               'password' => bcrypt('123456'),
//           ]);
//           Auth::guard('admin')->login($admin);

           $admin_user=[
               'name' => $request->input('name'),
               'password' => $request->input('password')
           ];

           if (Auth::guard('admin')->attempt($admin_user)) {
//               dd(Auth::guard('admin')->user(),session()->all());
               return redirect($this->redirectTo);
           }else{
               $request->session()->flash('status', '账号或密码错误！');
           }
       }

        return view('admin.Public.login');
    }

    /**
     * 退出登陆
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function logout(Request $request){
        Auth::logout();
        return redirect()->route("admin_login");
    }
}
